<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.cese.duss.ceseutil.CostUtil" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="rs1" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%
    bb.writeLog( "in----------------getHotelDtl_Ajax" );
    String yshd = Util.null2String( request.getParameter( "yshd" ) );
    String sqrq = Util.null2String( request.getParameter( "sqrq" ) );
    String cblrzx = Util.null2String( request.getParameter( "cblrzx" ) );
    String requestid = Util.null2String( request.getParameter( "requestid" ) );
    String yslx = Util.null2String( request.getParameter( "yslx" ) );
    JSONObject jsonData = new JSONObject();
    boolean flag = true;
    String msg = "";
    String jsonArr = Util.null2String( request.getParameter( "jsonArr" ) );
    JSONArray jsonArray = JSONArray.parseArray( jsonArr );
    JSONArray newjsonArray = delRepeatIndexid( jsonArray );
    for (int i = 0; i < newjsonArray.size(); i++) {
        String fykm = (String) newjsonArray.getJSONObject( i ).get( "fykm" );////费用科目
        String yjfy = (String)newjsonArray.getJSONObject( i ).get( "yjfy" );//预计费用
        double newyjfy = "".equals(yjfy)? 0 :Double.valueOf(yjfy);
        double jdcbkyys = CostUtil.getYszxxKyys( yshd, fykm, cblrzx, sqrq);//预算可用金额
        double ysje = CostUtil.getYszxxxbYsje(requestid, yshd, fykm, cblrzx, sqrq, yslx);
        String fykmName = CostUtil.getFykmId2Name(fykm);//获取费用科目名称
        if ( jdcbkyys == 0) {
            flag = false;
            msg = "提示：费用科目为["+fykmName+"]没有季度成本可用预算";
        } else {
            if (((jdcbkyys  -ysje)- newyjfy) < 0) {
                flag = false;
                msg = "提示：预计费用不允许大于季度成本可用预算";
            }
        }

    }
    jsonData.put( "flag", flag );
    jsonData.put( "msg", msg );
    out.print( jsonData.toJSONString() );
    bb.writeLog( "------------getHotelDtl_Ajax=" + jsonData.toJSONString() );
%>
<%!
    //根据Key去重复合并值
    public static JSONArray delRepeatIndexid(JSONArray array) {
        JSONArray arrayTemp = new JSONArray();
        int num = 0;
        for(int i = 0;i < array.size();i++){
            if(num==0){
                arrayTemp.add(array.get(i));
            }else{
                int numJ = 0;
                for(int j = 0;j < arrayTemp.size(); j++){
                    JSONObject newJsonObjectI = (JSONObject)array.get(i);
                    JSONObject newJsonObjectJ = (JSONObject)arrayTemp.get(j);
                    String index_idI = newJsonObjectI.get("fykm").toString();
                    String valueI = newJsonObjectI.get("yjfy").toString();
                    String index_idJ = newJsonObjectJ.get("fykm").toString();
                    String valueJ = newJsonObjectJ.get("yjfy").toString();
                    if(index_idI.equals(index_idJ)){
                        double newValue = Util.getDoubleValue(valueI) + Util.getDoubleValue(valueJ);
                        arrayTemp.remove(j);
                        JSONObject newObject = new JSONObject();
                        newObject.put("fykm", index_idI);
                        newObject.put("yjfy", newValue);
                        arrayTemp.add(newObject);
                        break;
                    }
                    numJ++;
                }
                if((numJ-1) == (arrayTemp.size()-1)){
                    arrayTemp.add(array.get(i));
                }
            }
            num++;
        }
        return arrayTemp;
    }
%>

